class KRuleCenter {
    companion object{
        val INSTANCE = KRuleCenter()
    }

    fun isWin(location:KLocation):Boolean{
        val row = location.row - 1
        val col = location.col - 1
        val boards = BoardData.boards
        val chess = boards[row][col] //刚落的棋子

        //从col开始遍历 ——> 最大5个 获取连续的个数 发现不一样 横向的遍历结束 得到右边连续的个数
        //遍历横向右
        var count = 1
        for (i in col+1..col+4){
            if (i == COLS) break //到达边界
            if (boards[row][i] != chess){
                break //右边的遍历结束
            }else{
                count++ //连续的个数+1
            }
        }
        //遍历横向左
        for (i in col-1 downTo col - (5-count) ){
            if (i == -1) break //到达左边界
            if (boards[row][i] != chess){
                break //左边的遍历结束
            }else{
                count++ //连续的个数+1
            }
        }

        if (count == 5) return true

        /**
         * 纵向
         */
        count = 1
        for (i in row+1..row+4){
            if (i == COLS) break //到达边界
            if (boards[i][col] != chess){
                break //右边的遍历结束
            }else{
                count++ //连续的个数+1
            }
        }
        //遍历横向左
        for (i in row-1 downTo row - (5-count) ){
            if (i == -1) break //到达左边界
            if (boards[i][col] != chess){
                break //左边的遍历结束
            }else{
                count++ //连续的个数+1
            }
        }

        if (count == 5) return true

        //右斜向
        count = 1
        for (i in 1..4){
            val x = col + i
            val y = row - i
            if (y == -1 || x == COLS) break //到达边界
            if (boards[x][y] != chess){
                break
            }else{
                count++ //连续的个数+1
            }
        }
        for (i in 1..5-count){
            val x = col - i
            val y = row + i
            if (y == ROWS || x == -1) break //到达边界
            if (boards[x][y] != chess){
                break
            }else{
                count++ //连续的个数+1
            }
        }
        if (count == 5) return true

        //左斜向
        count = 1
        for (i in 1..4){
            val x = col + i
            val y = row + i
            if (y == ROWS || x == COLS) break //到达边界
            if (boards[x][y] != chess){
                break
            }else{
                count++ //连续的个数+1
            }
        }
        for (i in 1..5-count){
            val x = col - i
            val y = row - i
            if (y == -1 || x == -1) break //到达边界
            if (boards[x][y] != chess){
                break
            }else{
                count++ //连续的个数+1
            }
        }

        return count == 5
    }

    fun isFull():Boolean{
        for (i in 0..<ROWS){
            for (j in 0..<COLS){
                if (BoardData.boards[i][j] == "+")
                    return false
            }
        }
        return true
    }
}